package edu.wust;

public class exam34 {
    public static boolean curiosity(int num){
        int temp=num,sum=0;
        while(temp!=0){
            sum+=mul(temp%10);
            temp/=10;
        }
        if(sum==num)
            return true;
        return false;
    }
    public static int mul(int num){ //阶乘函数
        if(num==0)
            return 1;
        int t=num;
        for(int i=2;i<num;++i)
            t*=i;
        return t;
    }
    public static void main(String[] args) {
        int sum=0;
        for(int i=3;i<41000;++i){
            if(curiosity(i)){
                sum+=i;
        }}
            System.out.println(sum);
    }
}

